//
//  LyricTool.m
//  Project_ZFR_MUSIC
//
//  Created by 张福润 on 16/2/27.
//  Copyright © 2016年 张福润. All rights reserved.
//

#import "LyricTool.h"
static LyricTool *ly=nil;
@implementation LyricTool
+(instancetype)shareLyricTool{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ly=[[LyricTool alloc]init];
    });
    return ly;
}

-(void)lyricWithStr:(NSString *)str{
    //如果有存放数据类型的数据,全部移除掉
    if(self.modelArr.count){
        [self.modelArr removeAllObjects];
    }
    
    if(str.length<1){
        return;
    }
    //根据/n切割
    NSArray *lyArr=[str componentsSeparatedByString:@"\n"];
    for(NSString *singleStr in lyArr) {
        if(!singleStr.length){
            continue;
        }
        NSArray *singleArr=[singleStr componentsSeparatedByString:@"]"];
        //第一个元素:s
        NSString *time=nil;
        if([(NSString *)singleArr.firstObject length]){
            time=[(NSString *)singleArr.firstObject substringFromIndex:1];
        }
        NSString *lyric=singleArr.lastObject;
        
        
        LyricTool *tool=[[LyricTool alloc]init];
        tool.lyric=lyric;
        tool.time=[self timeFromWithStr:time];
        [self.modelArr addObject:tool];
    }
    
}


-(NSInteger)indexForTime:(float)time{
    int i=0;
    for(LyricTool *tool in self.modelArr){
        
        if(tool.time>time){
            
            break;
        }
        i++;
        if(i<0){
            return 0;
        }
        
    }
    return i-1;
}
-(float)timeFromWithStr:(NSString *)str{
    NSArray *arr=[str componentsSeparatedByString:@":"];
    //秒数
    float time=[arr.firstObject integerValue]*60.0+[arr.lastObject floatValue];
    return time;
}

-(NSMutableArray *)modelArr{
    if(!_modelArr){
        _modelArr=[NSMutableArray array];
    }
    return _modelArr;
}


@end
